/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.velocityme.session;

import com.velocityme.entity.ChangeDelta;
import com.velocityme.entity.Group;
import com.velocityme.entity.GroupAccess;
import com.velocityme.entity.Node;
import com.velocityme.entity.Role;
import com.velocityme.entity.User;
import com.velocityme.enums.ChangeDeltaItemType;
import com.velocityme.enums.ChangeDeltaType;
import com.velocityme.enums.Permission;
import com.velocityme.utility.PermissionCache;
import java.util.HashSet;
import java.util.Set;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author rcrida
 */
@Stateless
public class GroupAccessBean implements GroupAccessLocal {

    @PersistenceContext
    private EntityManager em;

    public GroupAccess createGroupAccess(KeyLocal key, Long nodeId, Long groupId, Long roleId) {
        Node node = em.find(Node.class, nodeId);
        key.assertHasPermission(node, Permission.GROUP_ACCESS_CREATE);
        Group group = em.find(Group.class, groupId);
        Role role = em.find(Role.class, roleId);
        GroupAccess groupAccess = new GroupAccess(node, group, role);
        // clear the permission cache for the users in the group
        for (User user: group.getUsers()) {
            PermissionCache.getInstance().clearUser(user.getId());
        }
        ChangeDelta changeDelta = node.addChangeDelta(ChangeDeltaType.CREATE_GROUP_ACCESS, key.getUser());
        changeDelta.addItem(ChangeDeltaItemType.NODE, node.getPathName());
        changeDelta.addItem(ChangeDeltaItemType.ROLE, role.getName());
        em.persist(groupAccess);
        em.merge(group);
        em.merge(node);
        em.persist(changeDelta);
        return groupAccess;
    }

    public void deleteGroupAccess(KeyLocal key, Long groupAccessId) {
        GroupAccess groupAccess = em.find(GroupAccess.class, groupAccessId);
        key.assertHasPermission(groupAccess.getNode(), Permission.GROUP_ACCESS_DELETE);
        ChangeDelta changeDelta = groupAccess.getNode().addChangeDelta(ChangeDeltaType.DELETE_GROUP_ACCESS, key.getUser());
        changeDelta.addItem(ChangeDeltaItemType.NODE, groupAccess.getNode().getPathName());
        changeDelta.addItem(ChangeDeltaItemType.ROLE, groupAccess.getRole().getName());
        // clear the permission cache for the users in the group
        Group group = groupAccess.getGroup();
        for (User user: group.getUsers()) {
            PermissionCache.getInstance().clearUser(user.getId());
        }
        em.persist(changeDelta);
        em.remove(groupAccess);
    }

    public Set<GroupAccess> getGroupAccessByNode(KeyLocal key, Node node) {
        key.validateKey();
        Set<GroupAccess> accesses = new HashSet<GroupAccess>();
        while(node != null) {
            accesses.addAll(em.createNamedQuery("findGroupAccessByNode").setParameter("node", node).getResultList());
            node = node.getParentNode();
        }
        return accesses;
    }
    
    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method" or "Web Service > Add Operation")
 
}
